﻿@{
	PageData["Title"] = "Edit SKU";
    
    WebSecurity.RequireAuthenticatedUser();
    if (!Roles.IsUserInRole("admin") || UrlData[0].IsEmpty()) {
        Response.Redirect("~");
    }
    
	var price = 0.0M;
	var description = "";	
	var name = UrlData[0];
    var errorMessage = "";
        
    if(IsPost) {
        if (Request["action"] == "Update") {
            if (name.IsEmpty()) {
                errorMessage = "Name cannot be empty.";
            }
            
            //If no errors in validation, continue with registration
            if (errorMessage.IsEmpty()) {
                try {
                    //Update user information using Freemium Helper
                    name = Request["name"];
                    price = Request["price"].AsDecimal();
                    description = Request["description"];
                    byte[] fileBytes = null;
                    HttpPostedFileBase file = Request.Files[0];   
                    if (file.ContentLength > 0) {
                        var fileUpload = new WebImage(file.InputStream);
                        fileBytes = fileUpload.GetBytes();
                    }
                    
                    if(Request["remove"].AsBool()==true) {
                         Freemium.UpdateStockKeepingUnit(name, description, price, true, null);
                    } else {
                        if(fileBytes!=null) {
                            Freemium.UpdateStockKeepingUnit(name, description, price, true, fileBytes);
                        } else{
                            Freemium.UpdateStockKeepingUnit(name, description, price);
                        }
                    }
                    Response.Redirect("~/Freemium/Admin");
                } catch (Exception ex) {
                    errorMessage = "Sorry, we ran into a problem processing your request.";
                }
            }
        } else if (Request["action"] == "Delete") {
            name = Request["name"];
            Freemium.RemoveStockKeepingUnit(name);
            Response.Redirect("~/Freemium/Admin");
        }
    } else {
        name = UrlData[0];
        var sku = Freemium.GetStockKeepingUnit(name);
        
        name = sku.Name;
        price = sku.Price;
        description = sku.Description;
    }
}

@if (!errorMessage.IsEmpty()) {
<div class="error">
    @errorMessage
</div>
}

<h1>Edit SKU</h1>
Use the form below to modify the SKU.<br/>
<script type="text/javascript">
	function deleteSKU(name) {
        if (confirm("Are you sure you want to delete the SKU '" + name +  "' ?"))
        {
            document.forms['deleteSKUForm'].submit();
        }
	}
</script>
<div>
    <form id="validateForm" method="post" action="" enctype="multipart/form-data">
	    <fieldset>
		    <legend>Edit SKU</legend>
			<div class="freemiumLabel">
				<label class="" for="name">Name</label>
			</div>
			<div class="freemiumInput">
                <input name="name" type="hidden" value="@name" />
				<input name="name" type="text" value="@name" disabled />
			</div>
			<div class="freemiumLabel">
				<label for="price">Price</label>
			</div>
			<div class="freemiumInput">
				<input id="price" name="price" type="text" maxlength="5" value="@price" />
			</div>	
			<div class="freemiumLabel">
				<label for="description">Description</label>
			</div>
			<div class="freemiumInput">
				<input id="description" name="description" type="text" maxlength="1000" value="@description" />
			</div>
            
             <h2>Image</h2>            
            @Freemium.GetStockKeepingUnitImageHtml(name)            
            @FileUpload.GetHtml(allowMoreFilesToBeAdded:false, includeFormTag:false)
			<div class="freemiumInput">
				<input id="remove" name="remove" type="checkbox" value="true" />
                <label for="remove">Remove Image</label>
			</div>
            
            <div class="submit-group">
                <input type="submit" name="action" value="Update" />
                <input type="button" onclick="javascript:deleteSKU('@name')" value="Delete"> </input>
                <input type="button" name="action" value="Cancel" onclick="javascript:window.location='@Href("~/Freemium/Admin")'" />
            </div>
        </fieldset>
    </form>
</div>
 <form id="deleteSKUForm" method="post" action="">
    <input name="name" type="hidden" value="@name" />
    <input name="action" type="hidden" value="Delete"/>
</form>